import { ajax, src, util } from "../../utils/util.js";

Page({

  /**
   * 页面的初始数据
   */
  data: {
    // imgUrls: [
    //   'https://images.unsplash.com/photo-1551334787-21e6bd3ab135?w=640',
    //   'https://images.unsplash.com/photo-1551214012-84f95e060dee?w=640',
    //   'https://images.unsplash.com/photo-1551446591-142875a901a1?w=640'
    // ],
    // indicatorDots: false,
    // autoplay: false,
    // interval: 5000,
    // duration: 1000,
    // current:'',
    uqid:'',
    id:1,
    type:"1",
    
    mdetails:[],
    sindex:1, 
    sex:1,
    sex1:1, 
    yxsbk:-1,
    riqi:'2020-02-12',
    name:'',
    idcard:'',
    phone:'',
    showyuyuetc:false,
    showsettimetc:false,
    showtc11:false,

    riqilist:'',
    fenyuelist:'',
    selectyue:'0',
    shebaoka:'',
    miaoshu:"",

  },
  //轮播图滑动获取下标
  bindChange(e) {
    console.log(e.detail.current)
    this.setData({
      current: e.detail.current
    })
  },
  showtcc(){
    var that=this
    that.setData({
      showtc11:true
    })
    setTimeout(function(){
      that.setData({
        showtc11:false
      })
    },2000)
  },
  tobind(){
    wx.navigateTo({
      url: '../bindybk/bindybk',
    })
  },
  tuichusettime(){
    this.setData({
      showsettimetc:false
    })
  },

  getguanggao: function () {
    ajax("/api/index/config_show", {
      data: {
        key:'ydt_desc'
      },
      header: {
        'Accept': 'application/json', // 默认值
      },
      method: 'get'
    }, res => {

      this.setData({
        miaoshu: res.data
      })
    })
  },
  showtimetc(){
    this.setData({
      showsettimetc:true
    })
  },
   

  tuichuyuyue(){
    this.setData({
      showyuyuetc:false
    })
  },
  selectyueindex(e){
    var index=e.currentTarget.dataset.index
    this.setData({
      selectyue:index
    })
  },

  setsex(e){
    var index=e.currentTarget.dataset.index
    console.log(index)
    this.setData({
      sex:index
    })
  },
  setsex1(e){
    var index=e.currentTarget.dataset.index
    console.log(index)
    this.setData({
      sex1:index
    })
  },
  getsbk: function () {
    ajax("/api/index/social_card_show", {
      data: {
        uqid: this.data.uqid,
      },
      header: {
        'Accept': 'application/json', // 默认值
      },
    }, res => {
      console.log(res);
      if (res.error == 0) {
        this.setData({
          shebaoka: res.data,
        })
      }
    })
  },

  toyuyue(e){
    console.log(e)
    var riqi=e.currentTarget.dataset.date
    
    this.setData({
      showyuyuetc:true,
      riqi:riqi,
      showsettimetc:false,
    })
  },
  setyxsbk(e){
    var index=e.currentTarget.dataset.index
    console.log(index)
    if(index==0){
      this.setData({
        yxsbk:false
      })
    }
    if(index==1){
      this.setData({
        yxsbk:true
      })
    }
  },


  getname:function(e){
    console.log(e)
    this.setData({
      name:e.detail.value
    })
  },
  getidcard:function(e){
    console.log(e)
    this.setData({
      idcard:e.detail.value
    })
  },
  getphone:function(e){
    console.log(e)
    this.setData({
      phone:e.detail.value
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var uqid = wx.getStorageSync("uqid");
    var id = options.id;
    var type = options.type;
    this.setData({
      uqid: uqid,
      id:id,
      type:type,
    });
    if(type==1){
      wx.setNavigationBarTitle({
        title: '检查' 
      })
    }
    if(type==2){
      wx.setNavigationBarTitle({
        title: '复诊' 
      })
    }
    if(type==3){
      wx.setNavigationBarTitle({
        title: '治疗' 
      })
    }

    this.getsbk()

    this.getguanggao()
  },

  getcurrentday(){
    var myDate = new Date();
    var time = myDate.toLocaleDateString().split('/');
    if(time[1]<10){
      time[1]='0'+time[1]
    }
    if(time[2]<10){
      time[2]='0'+time[2]
    }
    time=time.join('-')
    return time
  },

  mechDetails:function(){
    var that=this
    console.log(111)
    var strapi,data
    if(this.data.type==2){
      strapi='/api/index/experts_show'
      data={
        id: that.data.id,
      }
    }else{
      strapi='/api/index/mechanism_show'
      data={
        id: that.data.id,
        type:that.data.type
      }
    }
    ajax(strapi, {
      data: data,
      header: {
        'Accept': 'application/json', // 默认值
      },
      method: 'GET'
    }, res => {
      console.log(res);
      if (res.error == 0) {

        var yuelist=[]
        var riqilist=res.data.getData

        var current=that.getcurrentday()
        console.log(current)
        for(let i=0;i<riqilist.length;i++){
          riqilist[i].yue= riqilist[i].date.substring(0,7)
          riqilist[i].ri= riqilist[i].date.substring(8,10)
          riqilist[i].riqi= riqilist[i].date.substring(5,10)
          riqilist[i].iscurrent=false
          
          console.log(riqilist[i].date.substring(0,7))
          if(yuelist.indexOf(riqilist[i].date.substring(0,7))==-1){
            yuelist.push(riqilist[i].date.substring(0,7))
          }
          
          // console.log(riqilist[i].date)
          if(riqilist[i].date == current){
            console.log(current)
            riqilist[i].iscurrent = true
          }
        }

        that.setData({
          mdetails: res.data.info,
          riqilist:riqilist,
        });
        // console.log(mdetails)
        

       
        console.log(yuelist)
        var fenyuelist=[]
        for(let i=0;i<yuelist.length;i++){
          var obj={
            yue:yuelist[i],
            list:[]
          }
          fenyuelist.push(obj)
        }

        for(let i=0;i<that.data.riqilist.length;i++){
          
          for(let l=0;l<fenyuelist.length;l++){
            if(String(that.data.riqilist[i].yue)==String(fenyuelist[l].yue)){
              fenyuelist[l].list.push(that.data.riqilist[i])
            }
          }
        }
        var ci
        var zhouji=fenyuelist[0].list[0].week
        console.log(zhouji)
        if(zhouji=='周日'){
          ci=6
        }
        if(zhouji=='周六'){
          ci=5
        }
        if(zhouji=='周五'){
          ci=4
        }
        if(zhouji=='周四'){
          ci=3
        }
        if(zhouji=='周三'){
          ci=2
        }
        if(zhouji=='周二'){
          ci=1
        }
        if(zhouji=='周一'){
          ci=0
        }
       console.log(ci)
        for(let i=0;i<ci;i++){
          fenyuelist[0].list.unshift('')
        }

        var ci2
        var zhouji2=fenyuelist[1].list[0].week
        console.log(zhouji2)
        if(zhouji2=='周日'){
          ci2=6
        }
        if(zhouji2=='周六'){
          ci2=5
        }
        if(zhouji2=='周五'){
          ci2=4
        }
        if(zhouji2=='周四'){
          ci2=3
        }
        if(zhouji2=='周三'){
          ci2=2
        }
        if(zhouji2=='周二'){
          ci2=1
        }
        if(zhouji=='周一'){
          ci2=0
        }
        console.log(ci2)
       
        for(let i=0;i<ci2;i++){
          fenyuelist[1].list.unshift('')
        }
        
        that.setData({
          fenyuelist:fenyuelist
        })
      }
    })
  },

  phoneCall: function (e) {
    var phone = e.currentTarget.dataset.phone;
    if (phone != undefined || phone != ''){
      wx.makePhoneCall({
        phoneNumber: phone,
        success: function () {
        },
      })
    }
  },

  tomap:function(){
    // console.log(this.data.mdetails.longitude)
    // wx.navigateTo({
    //   url: '/pages/map/map?longitude=' + this.data.mdetails.longitude + "&latitude=" + this.data.mdetails.latitude,
    // })
    if(this.data.type!=2){
      var latitude = Number(this.data.mdetails.latitude)
      var longitude = Number(this.data.mdetails.longitude)
      var name = this.data.mdetails.name
      var address = this.data.mdetails.address
    }
    if(this.data.type==2){
      var latitude = Number(this.data.mdetails.mechanism_latitude)
      var longitude = Number(this.data.mdetails.mechanism_longitude)
      var name = this.data.mdetails.mechanism_name
      var address = this.data.mdetails.mechanism_address
    }
    wx.openLocation({
      latitude,
      longitude,
      scale: 18,
      name,
      address
    })
  },
  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var uqid = wx.getStorageSync('uqid');
    this.setData({
      uqid:uqid
    })
    this.mechDetails();
    this.getsbk()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  yuyue(){
    // name: this.data.name,
    // sex:this.data.sex,
    // id_cards:this.data.idcard,
    // phone:this.data.phone,
    // re_time:this.data.riqi,
    // scid:scid
    var  that=this 

    if(this.data.name==''){
      wx.showToast({
        title: '名字不能为空',
        icon: 'none',
        duration: 1000
      })
      return
    }
    if(this.data.idcard==''){
      wx.showToast({
        title: '身份证号不能为空',
        icon: 'none',
        duration: 1000
      })
      return
    }
    if(this.data.phone==''){
      wx.showToast({
        title: '手机号不能为空',
        icon: 'none',
        duration: 1000
      })
      return
    }
    var scid
    if(this.data.yxsbk==true){
      scid=this.data.shebaoka.info.card_number
    }else{
      scid=''
    }

    ajax("/api/index/reserve", {
      data: {
        uqid: this.data.uqid,
        type:this.data.type,
        sid:this.data.mdetails.id,
        name: this.data.name,
        sex:this.data.sex,
        id_cards:this.data.idcard,
        phone:this.data.phone,
        re_time:this.data.riqi,
        scid:scid,
        is_treat:this.data.sex1,
      },
      header: {
        'Accept': 'application/json', // 默认值
      },
      method:'POST'
    }, res => {
      // console.log(res);
      if (res.error == 0) {
        wx.showToast({
          title: '预约成功',
          icon: 'none',
          duration: 1000
        })
        wx.navigateTo({
          url: '../wodeyuyue/wodeyuyue?type=' + that.data.type,
        })
      }else{
        wx.showToast({
          title: res.message,
          icon: 'none',
          duration: 1000
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})